package com.jcl.cloud.services.crawler.email.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jcl.cloud.common.service.util.HttpClientUtil;
import com.jcl.cloud.common.service.util.PageOperation;
import com.jcl.cloud.services.crawler.email.service.RedisService;
import com.jcl.cloud.services.crawler.email.util.SerializeUtils;
import com.jcl.cloud.services.crawler.email.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class QcwyCatchEmailHandler implements CatchEmailHandler {

    private final static Logger logger = LoggerFactory.getLogger(QcwyCatchEmailHandler.class);
    @Autowired
    private RedisService redisService;

    @Override
    public void execute(String crawlerId) {


        try {
            String email = this.getEmailAddr(crawlerId);
            this.searchByEmail(crawlerId, email);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            long currentTime = System.currentTimeMillis();
            long nextExecuteTime = currentTime + 180000;
            this.redisService.hset("unicrawler:task:info:" + crawlerId, "nextExecuteTime", String.valueOf(nextExecuteTime));
            //释放任务
            this.redisService.lpush("unicrawler:tasks:all", crawlerId);
        }
    }


    private String getEmailAddr(String crawlerId) {

        String email = this.redisService.rpop("crawler:brain:catch-email:tasks:" + crawlerId);

        if (StringUtils.isBlank(email)) {
            String url = "http://www.jucailin.com/api/user/find?ishr=1";
            String result = HttpClientUtil.doGet(url);
            logger.info("前程无忧获取到的hr和邮箱的对应关系为：" + result);
            //处理result获取email列表
            JSONObject jo = JSON.parseObject(result);
            JSONArray ja = jo.getJSONArray("users");
            for (Object object : ja) {
                JSONObject jsonob = (JSONObject) object;
                email = jsonob.getString("email");
                if (StringUtils.isNotBlank(email)) {
                    this.redisService.lpush("crawler:brain:catch-email:tasks:" + crawlerId, email);
                }
            }
        }

        return email;
    }


    private void searchByEmail(String crawlerId, String email) {

        try {
            if (StringUtils.isNotBlank(email)) {
                String cookieStoreStr = this.redisService.hget("unicrawler:task:info:" + crawlerId, "cookieStore");

                BasicCookieStore cookieStore = SerializeUtils.serializeToObj(cookieStoreStr);
                HttpClientContext context = HttpClientContext.create();
                context.setCookieStore(cookieStore);


                Map map = new HashMap();
                String url = "https://ehire.51job.com/Jobs/JobSearchPost.aspx";
                String Referer = "https://ehire.51job.com/Navigate.aspx?ShowTips=11&PwdComplexity=N";
                map.put("url", url);
                map.put("Referer", Referer);
                String result = PageOperation.doGet2JobSearchPost(map, context);
                Thread.sleep((long) (Math.random() * 30) * 1000);


                String strKey = StringUtil.getStrWithRegex(result, "var strKey = \"(.*?)\";");
                Referer = "https://ehire.51job.com/Jobs/JobSearchPost.aspx";
                map = new HashMap();
                map.put("logType", "event");
                map.put("pageNo", "43");
                map.put("memo", "");
                map.put("eventNo", "1");
                map.put("Key", strKey);
                map.put("rnd", Math.random());
                map.put("Referer", Referer);
                result = PageOperation.doGlobalActionLogAjax(map, context);
                Thread.sleep((long) (Math.random() * 30) * 1000);

                map = new HashMap();
                url = "https://ehire.51job.com/Jobs/JobSearchPost.aspx?IsHis=Y";
                Referer = "https://ehire.51job.com/Jobs/JobSearchPost.aspx";
                map.put("url", url);
                map.put("Referer", Referer);
                result = PageOperation.doGet2JobSearchPost(map, context);


                Thread.sleep((long) (Math.random() * 30) * 1000);

                int times = 0;
                String jobIdsBak = "";
                String resultBak = result;
                boolean flag = true;//标识是不是刚点到job展示列表

                do {
                    times++;
                    try {
                        Document document = Jsoup.parse(result);
                        String __VIEWSTATE = document.getElementById("__VIEWSTATE").attr("value");
                        String txt_keyword = email;
                        map = new HashMap();

                        if (flag) {
                            map.put("__EVENTTARGET", "btnSelect");
                            //变成已经进来的标识
                            flag = false;
                        } else {
                            map.put("__EVENTTARGET", "pagerBottomNew$nextButton");
                        }
                        map.put("__VIEWSTATE", __VIEWSTATE);
                        map.put("txt_keyword", txt_keyword);


                        result = PageOperation.doPost2JobSearchPost(map, context);

                        Thread.sleep((long) (Math.random() * 30 * 1000));
                        //解析result获取jobId
                        document = Jsoup.parse(result);
                        List<Element> eleList = document.getElementsByAttributeValue("id", "chkBox");
                        StringBuilder sb = new StringBuilder();

                        for (Element e : eleList) {
                            String attr = e.val();
                            sb.append(",51job" + attr.split("\\|")[0]);
                        }
                        String jobIds = sb.toString();
                        boolean status = StringUtils.isNotBlank(jobIds);
                        if (status) {
                            jobIds = jobIds.substring(1);
                            logger.info(String.format("email:%s 匹配的职位为:%s", email, jobIds));
                        } else {
                            logger.info(String.format("email:%s 匹配的职位为空：null", email));
                        }
                        if (!jobIds.equals(jobIdsBak) && status) {
                            jobIdsBak = jobIds;
                            //调接口入库
                            url = "http://www.jucailin.com/api/user/setotherjobrela";
                            map = new HashMap();
                            map.put("other_jids", jobIds);
                            map.put("user_email", email);
                            logger.info("*******前程无忧上传的jobIds = " + jobIds + "*******");
                            String retStr = HttpClientUtil.doPostJson(url, JSON.toJSONString(map));
                            logger.info("前程无忧上传邮箱和jobId的对应关系的返回：" + retStr);
                        } else {
                            break;
                        }
                        resultBak = result;
                        times = 0;
                    } catch (Exception e) {
                        Thread.sleep(5000);
                        result = resultBak;
                        logger.info("获取jobIds发生异常：", e);

                        if (times > 20) {

                            logger.error("前程无忧网站通过email获取职位对应关系发生异常,重试超过次数，系统退出");
                        }
                    }
                } while (true);
            }else {
                logger.info("前程无忧  获取到的email为null，无法进行匹配爬取");
            }
        } catch (Exception e) {
            logger.error("前程无忧网站通过email获取职位对应关系发生异常", e);
        }
    }
}
